<?php
class AddrModule{
	function index(){
		global $_G;
		$datas=$_G['db']->fetch_all("SELECT * FROM".table('address')."WHERE uid='".$_G['uid']."'");
		
		include template('uc/addr');
	}
	
	function add(){
		global $_G;
		$num=$_G['db']->result("SELECT count(*) FROM".table('address')."WHERE uid=".$_G['uid']);
		if($num>9){
			msg('你只能添加10条');
		}
		$region=$this->getaddr();
		include template('uc/addr');
	}
	function edit(){
		global $_G;
		$id=getGP('id','G','int');
		$result = $_G['db']->fetch("address",'*',array('id'=>$id,'uid'=>$_G['uid']));
		$region=$this->getaddr($result['rid']);
		include template('uc/addr');
	}
	function getaddrdetail($id){
		if(!$id)return ;
		
	}
	function getaddr($id){
		global $_G;
		if(!$id){
			$datas = $_G['db']->fetch_all("SELECT rid,region FROM".table("region")."WHERE pid=0");
		if(!empty($datas)){
			$html='<select onchange="region(this,1);" id="level1" name="level1"><option value="0">-请选择-</option>';
			foreach($datas as $p){
				$html .='<option value="'.$p['rid'].'">'.$p['region'].'</option>';
			}
			$html.='</select>';
		}
		}else{
			$rs=$_G['db']->fetch_one_array("SELECT pid,level FROM".table("region")."WHERE rid=".$id);
			$pid=$rs['pid'];
			if(!$pid)$pid=0;
			$level=$rs['level'];
			if(!$level)$level=0;
			
			$le=$level;
			$selected[$level]=$id;
			for($i=$level;$i>0;$i--){
				$datast[$i] = $_G['db']->fetch_all("SELECT rid,region FROM".table("region")."WHERE pid='$pid'");
				$selected[--$le]=$pid;
				$pid=$_G['db']->result("SELECT pid FROM".table("region")."WHERE rid='$pid'");
			}
		
		$datast[$level+1] = $_G['db']->fetch_all("SELECT rid,region FROM".table("region")."WHERE pid=".$id);
		if(empty($datast[$level+1]))unset($datast[$level+1]);
		ksort($datast);
		$i=1;
		foreach($datast as $p){
			$html.='<select onchange="region(this,'.$i.');" id="level'.$i.'" name="level'.$i.'"><option value="0">-请选择-</option>';
			foreach($p as $v){
				$html .='<option value="'.$v['rid'].'" '.($selected[$i]==$v['rid']?"selected":'').'>'.$v['region'].'</option>';
			}
			++$i;
			$html.='</select>';
		}		
		}
		return $html;
	}
	function ajax(){
		global $_G;
		$id=getGP('id','G','int');
		$datas = $_G['db']->fetch_all("SELECT rid,region,level FROM".table("region")."WHERE pid='$id'");
		
		if(!empty($datas)){
			foreach($datas as $p){
				$html .='<option value="'.$p['rid'].'">'.$p['region'].'</option>';
			}
			$data['data']='<select onchange="region(this,'.$p['level'].');" id="level'.$p['level'].'" name="level'.$p['level'].'"><option value="0">-请选择-</option>';
			$data['data'].=$html.'</select>';
		}
		$data['status']=true;
		print_R(json_encode($data));
	}
	function save(){
		global $_G;error_reporting(-1);
			if(getGP('addrsubmit','P') && $_G['uid']){
				$id=getGP('id','P','int');
				$data=array(
				'id'=>$id>0?$id:null,
				'uid'=>$_G['uid'],
				'rid'=>getGP('rid','P','int'),
				'firstname'=>getGP('firstname','P'),
				'zip'=>getGP('zip','P'),
				'street'=>getGP('street','P'),
				'street1'=>getGP('street1','P'),
				'phone'=>getGP('phone','P'),
				'tel'=>getGP('tel','P'),
				'email'=>getGP('email','P')
			);
			$_G['db']->insert('address',$data,true);
			msg('操作成功','home.php?m=addr');
		}
	}
}	